import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

// 获取原型对象上的push函数
const originalPush = Router.prototype.push
// 修改原型对象上的push方法
// location表示往哪里跳
Router.prototype.push = function push (location) {
  return originalPush.call(this, location).catch(error => error)
}

const originalReplace = Router.prototype.replace
Router.prototype.replace = function replace (location) {
  return originalReplace.call(this, location).catch(err => err)
}

export default new Router({
  routes: [
    {
      path: '/home',
      component: () => import('@/views/Home'),
      meta: { show: true }
    },
    {
      name: 'search',
      path: '/search/:keyword?',
      component: () => import('@/views/Search'),
      meta: { show: true }
    },
    {
      path: '/login',
      component: () => import('@/views/Login'),
      meta: { show: false }
    },
    {
      path: '/register',
      component: () => import('@/views/Register'),
      meta: { show: false }
    },
    {
      path: '*',
      redirect: '/home'
    }
  ]
})